package agency.sales ;

import java.security.MessageDigest ;
import java.util.Base64 ;
import java.util.Base64.Encoder ;
import java.util.Date ;
import java.util.HashMap ;
import java.util.Map ;
import java.util.TreeMap ;

import agency.Sales ;
import common.Http ;
import dict.AgencyDict ;
import dict.BcDict ;
import dicts.CountryDict ;
import entities.TBank ;
import entities.TBc ;
import entities.TCard ;
import entities.TTransaction ;
import net.sf.json.JSONObject ;
import util.LogUtil ;
import utils.AesUtil ;
import utils.DateUtil ;
import utils.DigestUtil ;
import utils.NumberUtil ;
import utils.StringUtil ;

public class Tosedc implements Sales
{

	private static final String PATH = "https://gate.cashrouter.com/CubePaymentGateway/action.NewSubmitAction.do" ;

	public static void main (
			String [ ] args
	) throws Exception
	{
		String aa = "1111222233334444hanzi000000022412344333156" ;
		System.out.println ( DigestUtil.md5 ( aa ) ) ;
		byte [ ] a = Base64.getEncoder ( ).encode ( MessageDigest.getInstance ( "MD5" ).digest ( aa.getBytes ( "utf-8" ) ) ) ;
		System.out.println ( new String ( a ) ) ;
	}

	public String sign (
			String text
	) throws Exception
	{

		try
		{
			MessageDigest md5 = MessageDigest.getInstance ( "MD5" ) ;
			Encoder baseEncoder = Base64.getEncoder ( ) ;
			String szHashValue = new String ( baseEncoder.encode ( md5.digest ( text.getBytes ( "UTF-8" ) ) ) ) ;
			return szHashValue ;
		} catch ( Exception e )
		{
			e.printStackTrace ( ) ;
			return null ;
		}

	}

	public void process (
			TBc bc ,
			TTransaction transaction ,
			TBank bank ,
			TCard card
	)
	{
		Map < String , String > requestMap = new TreeMap < String , String > ( ) ;
		JSONObject responseMap = new JSONObject ( ) ;
		String res = null ;
		String [ ] params = null ;
		// 消费

		requestMap.put ( "AcctNo" , bank.getBnkMer ( ) ) ;
		requestMap.put ( "URL" , bank.getBnkTer ( ) ) ;
		requestMap.put ( "OrderUrl" , bank.getBnkTer ( ) ) ;
		requestMap.put ( "OrderID" , bc.getBcNo ( ) ) ;
		requestMap.put ( "Amount" , NumberUtil.transfer ( bc.getBcAmount ( ) ).replace ( "." , "" ) ) ;
		requestMap.put ( "CurrCode" , "156" ) ;
		requestMap.put ( "IPAddress" , card.getCrdIp ( ) ) ;
		requestMap.put ( "Telephone" , card.getCrdCphone ( ) ) ;
		requestMap.put ( "CardType" , StringUtil.upperHead ( card.getCrdType ( ).toLowerCase ( ) ) ) ;
		requestMap.put ( "IFrame" , "1" ) ;
		requestMap.put ( "TxnType" , "01" ) ;
		requestMap.put ( "IVersion" , "V7.0" ) ;
		requestMap.put ( "PName" , transaction.getTxnProduct ( ) ) ;
		requestMap.put ( "Framework" , "customized" ) ;
		requestMap.put ( "CMSVersion" , "1.0.0" ) ;
		requestMap.put ( "PHPVersion" , "5.7" ) ;
		requestMap.put ( "Language" , "en" ) ;
		requestMap.put ( "callbackUrl" , "http://www.baidu.com" ) ;
		requestMap.put ( "Issuer" , card.getCrdHfname ( ) + " " + card.getCrdHlname ( ) ) ;
		requestMap.put ( "Email" , card.getCrdHemail ( ) ) ;
		requestMap.put ( "Bcountry" , card.getCrdHcountry ( ) ) ;
		requestMap.put ( "BCountryCode" , CountryDict.shortName ( card.getCrdHcountry ( ) ) ) ;
		requestMap.put ( "Bstate" , card.getCrdHstate ( ) ) ;
		requestMap.put ( "BCity" , card.getCrdHcity ( ) ) ;
		requestMap.put ( "BAddress" , card.getCrdHaddr ( ) ) ;
		requestMap.put ( "PostCode" , card.getCrdHzip ( ) ) ;

		try
		{
			String expire = AesUtil.AES_Decrypt ( AesUtil.KEY , card.getCrdExpire ( ) ) ;
			String cvv = AesUtil.AES_Decrypt ( AesUtil.KEY , card.getCrdCvv ( ) ) ;
			requestMap.put ( "CardPAN" , card.getCrdNumber ( ) ) ;
			requestMap.put ( "ExpDate" , expire.substring ( 2 , 4 ) + expire.substring ( 0 , 2 ) ) ;
			requestMap.put ( "CVV2" , cvv ) ;
			requestMap.put ( "HashValue" , sign ( bank.getBnkKey ( ) + requestMap.get ( "AcctNo" ) + requestMap.get ( "AcctNo" ) + requestMap.get ( "OrderID" ) + requestMap.get ( "Amount" ) + requestMap.get ( "CurrCode" ) ) ) ;
		} catch ( Exception e )
		{
			bc.setBcMark ( "555" ) ;
			bc.setBcInfo ( "edc sale data encrpt exception" ) ;
			bc.setBcCode ( AgencyDict.ETP_ENCRYPT_EXCEPTION ) ;
			bc.setBcMessage ( AgencyDict.AGENCY.get ( AgencyDict.ETP_ENCRYPT_EXCEPTION ) ) ;
			return ;
		}

		// 日志打印发送数据
		StringBuffer send = new StringBuffer ( ) ;
		for ( Map.Entry < String , String > e : requestMap.entrySet ( ) )
		{
			send.append ( e.getKey ( ) + "=" + e.getValue ( ) + "&" ) ;
			// System.out.println(e.getKey()+"==="+e.getValue());
		}
		LogUtil.agency ( "TOSEDC_SALES" , send.toString ( ) ) ;

		try
		{
			res = Http.post ( PATH , requestMap ) ;
			System.out.println ( res ) ;
		} catch ( Exception e )
		{
			e.printStackTrace ( );
			bc.setBcMark ( "555" ) ;
			bc.setBcInfo ( "edc sale data encrpt exception" ) ;
			bc.setBcCode ( AgencyDict.ETP_TRANSMISSION_EXCEPTION ) ;
			bc.setBcMessage ( AgencyDict.AGENCY.get ( AgencyDict.ETP_TRANSMISSION_EXCEPTION ) ) ;
			return ;
		}

		if ( StringUtil.isEmpty ( res ) )
		{
			bc.setBcMark ( "555" ) ;
			bc.setBcInfo ( "edc sale data response exception" ) ;
			bc.setBcCode ( AgencyDict.ETP_TRANSMISSION_EXCEPTION ) ;
			bc.setBcMessage ( AgencyDict.AGENCY.get ( AgencyDict.ETP_TRANSMISSION_EXCEPTION ) ) ;
			return ;
		}

		try
		{
			responseMap = JSONObject.fromObject ( res ) ;
		} catch ( Exception e )
		{
			bc.setBcMark ( "555" ) ;
			bc.setBcInfo ( "edc sale data disembly exception" ) ;
			bc.setBcCode ( AgencyDict.ETP_TRANSMISSION_EXCEPTION ) ;
			bc.setBcMessage ( AgencyDict.AGENCY.get ( AgencyDict.ETP_TRANSMISSION_EXCEPTION ) ) ;
			return ;
		}

		// 日志打印返回数据
		LogUtil.agency ( "ETPEDC_SALES" , res ) ;

		if ( ! responseMap.containsKey ( "statusCode" ) )
		{
			bc.setBcMark ( "555" ) ;
			bc.setBcInfo ( "edc sale data parameter exception" ) ;
			bc.setBcCode ( AgencyDict.ETP_TRANSMISSION_EXCEPTION ) ;
			bc.setBcMessage ( AgencyDict.AGENCY.get ( AgencyDict.ETP_TRANSMISSION_EXCEPTION ) ) ;
			return ;
		}

		if ( ! responseMap.get ( "statusCode" ).equals ( "success" ) )
		{
			bc.setBcMark ( responseMap.get ( "errorCode" ).toString ( ) ) ;
			bc.setBcInfo ( responseMap.get ( "errorCode" ).toString ( ) ) ;
			bc.setBcCode ( AgencyDict.ETP_SALES_FAILURE ) ;
			bc.setBcMessage ( AgencyDict.AGENCY.get ( AgencyDict.ETP_SALES_FAILURE ) ) ;
			return ;
		}

		// 如果成功
		Map < String , String > resultMap = new HashMap < String , String > ( ) ;
		params = responseMap.get ( "errorCode" ).toString ( ).split ( "&" ) ;
		for ( int i = 0 ; i < params.length ; i ++ )
		{
			String [ ] p = params [ i ].split ( "=" ) ;
			if ( p.length == 2 )
			{
				resultMap.put ( p [ 0 ] , p [ 1 ] ) ;
			}
		}

		bc.setBcState ( "1" ) ;
		bc.setBcRef ( resultMap.get ( "refund" ).toString ( ) ) ;
		bc.setBcCard ( card.getCrdFake ( ) ) ;
		bc.setBcTime ( DateUtil.format ( new Date ( ) , DateUtil.DATE ) ) ;
		bc.setBcMark ( responseMap.get ( "statusCode" ).toString ( ) ) ;
		bc.setBcInfo ( responseMap.get ( "statusCode" ).toString ( ) ) ;
		bc.setBcCode ( BcDict.BC_SUCCESS ) ;
		bc.setBcMessage ( BcDict.MESSAGES.get ( BcDict.BC_SUCCESS ) ) ;
		return ;
	}

}
